//
//  LLAlertViewController.m
//  MarketingApp
//
//  Created by 李叶夫 on 2018/4/3.
//  Copyright © 2018年 李叶夫. All rights reserved.
//

#import "LLAlertViewController.h"
#import "LLAlertControllerCell.h"

@interface LLAlertViewController ()<UITableViewDelegate,UITableViewDataSource,LLAlertControllerCellDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIButton *cancelButton;

@end

@implementation LLAlertViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor clearColor];
    [self setup];
    
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    self.tableView.frame = CGRectMake(15, self.controllerHeight-(self.actions.count+1)*48-15-kiPhoneXInsetBottom, self.view.width-30, self.actions.count*48);
    self.cancelButton.frame = CGRectMake(15, CGRectGetMaxY(self.tableView.frame)+10, CGRectGetWidth(self.tableView.frame), 48);
    
}

- (void)setup
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.rowHeight = 48;
    self.tableView.scrollEnabled = self.actions.count > 10 ? true : false;
    self.tableView.layer.cornerRadius = 4;
    self.tableView.layer.masksToBounds = true;
    [self.view addSubview:self.tableView];
    
    self.cancelButton = [[UIButton alloc] init];
    [self.cancelButton setTitleColor:UIColorFromRGB(0xef5350) forState:UIControlStateNormal];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
    self.cancelButton.backgroundColor = [UIColor whiteColor];
    [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"btn_status_highlighted2"] forState:UIControlStateHighlighted];
    [self.cancelButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    self.cancelButton.layer.cornerRadius = 4;
    self.cancelButton.layer.masksToBounds = YES;
    [self.view addSubview:self.cancelButton];
    
}

- (void)cancelAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (CGFloat)controllerHeight
{
    return kScreenH;
}

#pragma mark - LLAlertControllerCellDelegate

- (void)alertControllerCell:(LLAlertControllerCell *)cell didSelected:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.actionBlock) {
            NSInteger index = [self.tableView indexPathForCell:cell].row;
            self.actionBlock(index, button.titleLabel.text);
        }
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.actions.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LLAlertControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([self class])];
    if (!cell) {
        cell = [[LLAlertControllerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([self class])];
    }
    cell.actionTitle = self.actions[indexPath.row];
    if (self.actionImgs) {
        cell.actionImg = self.actionImgs[indexPath.row];
    }
    cell.delegate = self;
//    if (indexPath.row == self.selectedIndex) {
//        cell.y_selected = YES;
//    } else {
//        cell.y_selected = NO;
//    }
    return cell;
}



- (void)dealloc
{
    NSLog(@"%@销毁了",NSStringFromClass([self class]));
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
